<?php

namespace app\admin\model;

use think\Model;

/**
 * 活动
 * Class Marketing
 * @package app\admin\model
 */
class Marketing extends Model
{
    /**
     * 活动列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getMarketingList($where, $page, $pagesize)
    {
        $data = self::alias('m')
            ->join('location c', 'm.city = c.location_id', 'left')
            ->join('resource_official_staff o', 'm.manager = o.id', 'left')
            ->field('m.*,c.name as city_name,o.alias as manager_name')
            ->where($where)
            ->page($page, $pagesize)
            ->order('m.create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 活动列表统计
     * @param $where
     * @return mixed
     */
    public static function countMarketingList($where)
    {
        return self::alias('m')
            ->join('resource_official_staff o', 'm.manager = o.id', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 单活动
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findMarketing($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 创建活动
     * @param $data
     * @return Marketing|Model
     */
    public static function createMarketing($data)
    {
        return self::create($data);
    }

    /**
     * 更新活动
     * @param $data
     * @param $where
     * @return Marketing
     */
    public static function updateMarketing($data, $where)
    {
        return self::update($data, $where);
    }
}